package com.toobigtofail
{
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Emitter;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.masks.Pixelmask;
	

	public class BaseEnemy extends P2PEntity
	{
		[Embed(source="/assets/gfx/fetus.png")] private static const FETUS_PNG:Class;
		[Embed(source="/assets/gfx/pyramid.png")] private static const PYRAMID_PNG:Class;
		[Embed(source="/assets/gfx/pyramid.png")] private static const BIGPYRAMID_PNG:Class;
		[Embed(source="/assets/gfx/birdjar.png")] private static const BIRDJAR_PNG:Class;
		[Embed(source="/assets/gfx/monkeyjar.png")] private static const MONKEYJAR_PNG:Class;	
		[Embed(source="/assets/gfx/anibusjar.png")] private static const ANIBUSJAR_PNG:Class;
		[Embed(source="/assets/gfx/sideJar.png")] private static const SIDEJAR_PNG:Class;
		[Embed(source="/assets/gfx/Tut.png")] private static const TUT_PNG:Class;
		[Embed(source="/assets/gfx/totem.png")] private static const TOTEM_PNG:Class;
		[Embed(source="/assets/gfx/tempLocust.png")] private static const LOCUST_PNG:Class;
		[Embed(source="/assets/gfx/scarab.png")] private static const SCARAB_PNG:Class;
		
		public static const FAST_SPEED:Number = 200;
		public static const SLOW_SPEED:Number = 60;
		
		public static const DEATH_DECELERATION:Number = 40;
		
		public var speed:Number = 0;
		public var deceleration:Number = 0;
		private var img:Image;
		private var _sprMap:Spritemap
		public var enemyType:String;
		
		//specialized enemy variables
		private var verticalTrackingOn:Boolean = false;
		private var verticalTrackingTimer:Number = 0;
		private const FETUSVERT:int = 150;
		
		private var _locustsFired:Boolean = false;
		private const LOCUSTRANGE:int = 100;
		private var ySpeed:int = 0;
		private var _locustEmitter:Emitter;
		
		private var _isP2PMaster:Boolean;
		
		private var _jarStepper:int = 0;
		
		public function BaseEnemy(enemyType:String, xPos:int, yPos:int, speed:Number, isP2PMaster:Boolean)
		{
			_isP2PMaster = isP2PMaster;
			
			this.x = xPos + FP.width;
			this.y = yPos;
			
			this.enemyType = enemyType;
			switch(enemyType)
			{
				case "fetus": spawnFetus(); break;
				case "pyramid": spawnPyramid(); break;
				case "bigpyramid": spawnBigPyramid(); break;
				case "jar": spawnJar(); break;
				case "sidejar": spawnSideJar(); break;
				case "tut": spawnTut(); break;
				case "totem": spawnTotem(); break;
				case "locusts": spawnLocusts(); break;
				case "scarab": spawnScarab(); break;
				default: trace("Invalid Enemy Type");
			}
			this.type = "enemy";
			this.width = img.width;
			this.height = img.height;

			this.speed += speed; 
			if(this.enemyType == "locusts")
			{
				graphic = _locustEmitter;
				this.width = img.width;
				this.height = img.height;
			}
			else if(this.enemyType == "tut")
			{
				graphic = _sprMap;
				this.width = img.width/3;
				this.height = img.height;
			}
			else if(this.enemyType == "fetus")
			{
				graphic = _sprMap;
				this.width = img.width/3;
				this.height = img.height;
			}
			else if(this.enemyType == "jar")
			{
				graphic = _sprMap;
				this.width = img.width/12;
				this.height = img.height;
			}
			else
			{
				graphic = img;
				this.width = img.width;
				this.height = img.height;
			}
			img.originX = img.width / 2;
			img.originY = img.height / 2;
			
			_isP2PMaster = isP2PMaster;
			if (isP2PMaster) {
				this.autoAssignP2PId();
			}
		}
		
		override public function added():void {
			super.added();
			
			if (_isP2PMaster) {
				(world as Game).actionHandler.sendAction(GameAction.ADD_ENEMY, {p2pId: p2pId, x: x, y: y, type: enemyType, speed: speed, deceleration: deceleration});
			}
		}
		
		override public function update():void
		{
			speed -= deceleration * FP.elapsed;
			if (speed < 0) {
				speed = 0;
				deceleration = 0;
			}
			
			moveBy(-speed * FP.elapsed, ySpeed * FP.elapsed);
			
			if(x + this.width < 0 && enemyType != "locusts")
				FP.world.remove(this);
			else if(x + this.width + 400 < 0 && enemyType == "locusts")
				FP.world.remove(this);
			
			if(enemyType == "fetus")
			{
				if(this.x < FP.width  && verticalTrackingOn)
				{
					applyVerticalMovement();
					verticalTrackingOn = false;
					verticalTrackingTimer = 0;
				}
				if(!verticalTrackingOn)
				{
					verticalTrackingTimer += FP.elapsed;
					if(verticalTrackingTimer < 2)
					{
						verticalTrackingTimer = 0;
						verticalTrackingOn = true;
					}
					
				}
			}
			
			if(enemyType == "tut" && !_locustsFired)
			{
				if(this.x + this.width < FP.width - LOCUSTRANGE)
				{
					_sprMap.play("barf")
					
					if(_sprMap.currentAnim == "barf" && !_locustsFired)
					{
						if(_sprMap.frame == 3)
						{
							FP.world.add(new BaseEnemy("scarab", this.x, this.y, this.speed, this._isP2PMaster));
							_locustsFired = true;
						}
					}
				}
			}
			
			if(enemyType == "fetus")
			{
				_sprMap.play("crawl");
				
			}
			
			
			if(enemyType == "locusts")
			{
				
				_locustEmitter.emit("swarm", this.x, this.y);
			}
			
			if(enemyType == "jar")
			{
				_sprMap.setFrame(Math.floor((FP.width - Math.abs(this.x+this.width))/(FP.width/12)), 0);
				/*
				if(((FP.width - this.x)/(FP.width/12)) as int != _jarStepper)
				{
					FP.log(_jarStepper);
					_jarStepper = (this.x - FP.width)/(FP.width/12);
					if(_jarStepper >= 0 && _jarStepper <= 11)
					{
						_sprMap.setFrame(_jarStepper ,0);
					}
				}
				*/
			}
			super.update();
			
		}
		
		private function applyVerticalMovement():void
		{
			if(((FP.world) as Game)._player.y < this.y)
		
				moveBy(0, -FETUSVERT * FP.elapsed); 
		
			else
				moveBy(0, FETUSVERT * FP.elapsed);
			
		}
		
		
		private function spawnFetus():void
		{
			img = new Image(FETUS_PNG);
			_sprMap = new Spritemap(FETUS_PNG,40,73);
			_sprMap.add("crawl", [0,1,2,3,4,5],12,true);
			mask = new Pixelmask(FETUS_PNG);
			
			
			
			speed += 100;
			verticalTrackingOn = true;
		}
		
		private function spawnPyramid():void
		{
			img = new Image(PYRAMID_PNG);
			mask = new Pixelmask(PYRAMID_PNG);
		}
		
		private function spawnBigPyramid():void
		{
			img = new Image(BIGPYRAMID_PNG);
			mask = new Pixelmask(BIGPYRAMID_PNG);
		}
		
		private function spawnJar():void
		{
			var jarray:Array = new Array(/*{className: BIRDJAR_PNG, width:44 , height:100 }, */{className: ANIBUSJAR_PNG, width:44 , height:100 }, {className: MONKEYJAR_PNG, width:37 , height:100 });
			var x:int = FP.rand(jarray.length);
			img = new Image(jarray[x].className);
			_sprMap = new Spritemap(jarray[x].className, jarray[x].width, jarray[x].height);
			mask = new Pixelmask(jarray[x].className);
			_sprMap.setFrame(0 ,0);

			
		}
	
		private function spawnSideJar():void
		{
			img = new Image(SIDEJAR_PNG);
			mask = new Pixelmask(SIDEJAR_PNG);
		}
		
		private function spawnTut():void
		{
			img = new Image(TUT_PNG);
			_sprMap = new Spritemap(TUT_PNG,62,100);
			_sprMap.add("barf", [0,1,2,2,1,0],4,false);
			mask = new Pixelmask(TUT_PNG);
		}
		
		private function spawnTotem():void
		{
			img = new Image(TOTEM_PNG);	
			mask = new Pixelmask(TOTEM_PNG);
		}
		
		private function spawnLocusts():void
		{
			this.x -= FP.width;
			_locustEmitter = new Emitter(LOCUST_PNG, 7, 10);
			_locustEmitter.newType("swarm", [0]);
			//_locustEmitter.setMotion("swarm",-FP.angle(this.x, this.y, (FP.world as Game)._player.x,(FP.world as Game)._player.y),10,2,360, 10, 1);
			_locustEmitter.setMotion("swarm",0,10,1,360, 10, 1);
			
			img = new Image(LOCUST_PNG);
			speed += 100;
			ySpeed = -((this.y - (FP.world as Game)._player.y)/(this.x - (FP.world as Game)._player.x))* speed;
			
			
		}
		
		private function spawnScarab():void
		{
			this.x -= FP.width;
			img = new Image(SCARAB_PNG);
			mask = new Pixelmask(SCARAB_PNG);
			speed += 100;
			ySpeed = -((this.y - (FP.world as Game)._player.y)/(this.x - (FP.world as Game)._player.x))* speed;
		}
	}
}